import request from '@/utils/request';
import type { OptionTypes, ResponseData } from '@/utils/types.d';
import { SUCCESS_CODE } from '@/utils/types.d';
import type { CreateParkinglotType, ParkinglotTableListItem, UpdateParkingltType } from './types';

// 获取路段列表
export async function queryParkinglots(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: ParkinglotTableListItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: ParkinglotTableListItem[];
  }> = await request('/api/parkinglot/v1.0.0/parkinglots', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

// 获取巡检设备列表
export async function queryPatrolDevicsSN(deviceSn: string): Promise<OptionTypes[]> {
  const res: ResponseData<OptionTypes[]> = await request(
    '/api/patrol-device/v1.0.0/patrolDevices',
    {
      params: { deviceSn },
    },
  );
  if (res?.code === SUCCESS_CODE) {
    return res.data;
  }
  return [];
}

export async function createParkingLot(params: CreateParkinglotType) {
  const res: ResponseData<ParkinglotTableListItem> = await request(
    '/api/parkinglot/v1.0.0/parkinglot',
    {
      method: 'POST',
      data: params,
    },
  );
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '添加成功',
    };
  return {
    data: res?.data,
    success: false,
    message: res?.message || '添加失败',
  };
}

export async function createParkinglotNew(form: FormData) {
  const res: ResponseData<ParkinglotTableListItem> = await request('/api/parkinglot/v1.0.0/new', {
    method: 'POST',
    requestType: 'form',
    // body: form,
    data: form,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '添加成功',
    };
  return {
    data: res?.data,
    success: false,
    message: res?.message || '添加失败',
  };
}

export async function updateParkinglot(id: string, newInfo: UpdateParkingltType) {
  if (!id || !newInfo || Object.keys(newInfo).length === 0)
    return { success: false, message: '更新失败，请填入需要更新的信息' };
  const res: ResponseData<null> = await request(`/api/parkinglot/v1.0.0/parkinglot/${id}`, {
    method: 'PUT',
    data: newInfo,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data, message: '更新成功' };
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}

export async function updateParkinglotNew(form: FormData) {
  const res: ResponseData<{ imgs: string[] }> = await request('/api/parkinglot/v1.0.0/new', {
    method: 'PUT',
    requestType: 'form',
    // body: form,
    data: form,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '更新成功',
    };
  return {
    data: res?.data,
    success: false,
    message: res?.message || '更新失败',
  };
}

export async function removeParkinglot(id: string) {
  const res: ResponseData<null> = await request(`/api/parkinglot/v1.0.0/parkinglot/${id}`, {
    method: 'DELETE',
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '删除成功' };
  return { success: false, message: res?.message || '删除失败' };
}

// 获取泊位标记
/* export async function getSpotsMarkers(parkingLotId: string) {
  const res: ResponseData<{ location: number[]; parkingSpotId: string }[]> = await request(
    '/api/parkinglot/v2.0.0/spots-marker',
    {
      params: { parkingLotId },
    },
  );
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: res.data, message: '获取成功' };
  }
  return { success: false, data: [], message: res?.message || '获取失败' };
} */
export async function exportParkinglot(params: { ownerId?: string }) {
  return await request('/api/parkinglot/v1.0.0/export-parkinglot', {
    params,
    responseType: 'blob',
  });
}

export async function queryChargeRules(name: string): Promise<OptionTypes[]> {
  const res: ResponseData<OptionTypes[]> = await request('/api/charge-rule/v1.0.0/chargeRules', {
    params: { name },
  });
  if (res?.code === SUCCESS_CODE) {
    return res.data;
  }
  return [];
}

export async function deleteMulLot(parkingLotIds: string[]) {
  const res: ResponseData<null> = await request('/api/parkinglot/v1.0.0/mul-lot', {
    method: 'DELETE',
    data: {
      parkingLotIds,
    },
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '删除成功' };
  return { success: false, message: res?.message || '删除失败' };
}

export async function updateMulLot(parkingLotIds: string[], chargeRule: string) {
  const res: ResponseData<null> = await request('/api/parkinglot/v1.0.0/mul-chargerule', {
    method: 'PUT',
    data: {
      parkingLotIds,
      chargeRule,
    },
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data, message: '更新成功' };
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}
